package com.community.service.impl;

import com.community.pojo.EmployeeRole;
import com.community.mapper.EmployeeRoleMapper;
import com.community.pojo.Role;
import com.community.service.IEmployeeRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 员工角色关联表 服务实现类
 * </p>
 *
 * @author TangHaoSong Fly ChenJie HeYuHang
 * @since 2024-05-15
 */
@Service
public class EmployeeRoleServiceImpl extends ServiceImpl<EmployeeRoleMapper, EmployeeRole> implements IEmployeeRoleService {

    @Autowired
    private EmployeeRoleMapper employeeRoleMapper;


    /**
     * 重写方法：批量为员工添加角色
     * @param employeeId 员工的ID
     * @param roles 包含要添加的角色信息的列表
     */
    @Override
    public void batchAddRoles(Integer employeeId, List<Integer> roles) {

        // 遍历传入的角色列表，为每个角色创建一个员工角色对象并插入数据库
        for (Integer role : roles) {
            // 创建员工角色对象
            EmployeeRole employeeRole = new EmployeeRole();
            employeeRole.setEmployeeId(employeeId);
            employeeRole.setRoleId(role);

            // 使用 employeeRoleMapper 插入数据
            employeeRoleMapper.insert(employeeRole);
        }
    }
}
